#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //实例化
    manager = new QNetworkAccessManager(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    //获取到输入框中的url，并构建一个url对象
    QUrl url(ui->lineEdit->text());
    //构造一个http请求对象
    QNetworkRequest request(url);
    //发送get请求，返回一个响应
    QNetworkReply* response = manager->get(request);
    //信号槽
    connect(response, &QNetworkReply::finished, this, [=](){
        //正确响应了
        if(response->error() == QNetworkReply::NoError)
        {
            QString html_data = response->readAll();
            ui->plainTextEdit->setPlainText(html_data);
        }
        else
        {
            ui->plainTextEdit->setPlainText(response->errorString());
        }
        //释放response对象
        response->deleteLater();
    });
}
